﻿var lblDateTime;
var lblDateID;
var lblLangID;
var timerID = null;
var timerRunning = false;
var dt = new Date();
window.onload = RenderTag;

function RenderTag() {
	ShowClock();
}

function SetTimes(years, months, days , hours, minutes, seconds) {
	// set day
	dt.setDate(days);
	// set month, day
	dt.setMonth(months, days);
	// set full year
	dt.setFullYear(years, months, days);
	// set time
	dt.setHours(hours, minutes, seconds);
    // restart time
	if(timerRunning)
    	clearTimeout(timerID)
    timerRunning = false;
}

function SetValue(agr1, agr2, agr3) {
    lblDateID=agr1;
    lblLangID=agr2;
    lblDateTime=agr3;
}

function ShowClock()
{
	// 1: dd-MM-yyyy
	// 2: dd-MMM-yyyy
	// 3: dd/MM/yyyy
	// 4: MM/dd/yyyy
	// 5: yyyy-MM-dd
	// 6: yy/MM/dd
	// 7: dd-MMM-yy
	// 8: MM/dd/yy
	// restart time
	if(timerRunning)
    	clearTimeout(timerID)
    timerRunning = false;
	// get control display date time
	var elem = document.getElementById(lblDateTime);
	// get status display date format
	var flag = document.getElementById(lblDateID);
	// declare
	var years = dt.getFullYear();
	var months = dt.getMonth();
	var days = dt.getDate();
	var hours = dt.getHours();
	var minutes = dt.getMinutes();
	var seconds = dt.getSeconds();
	var arrMonthName = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	/**************************************************/
	var startAt = 1;		// 0 - Sunday ; 1 - Monday
	var dayName = '';
	var lang = 'vi';
	if (document.getElementById(lblLangID) != null)
	    lang = document.getElementById(lblLangID).value;
	// zh: Chinese, vi: Vietnamese, en: English
	if (startAt==0) {
		dayName = {
			zh : new Array('星期天','星期一','星期二','星期三','星期四','星期五','星期六'),
			vi : new Array('Chủ nhật, ','Thứ Hai, ','Thứ Ba, ','Thứ Tư, ','Thứ Năm, ','Thứ Sáu, ','Thứ Bảy, '),
			en : new Array('Sunday, ','Monday, ','Tuesday, ','Wednesday, ','Thursday, ','Friday, ','Saturday, ')
		};
	} else {
		dayName = {
			zh : new Array('星期一','星期二','星期三','星期四','星期五','星期六','星期天'),
			vi : new Array('Thứ Hai, ','Thứ Ba, ','Thứ Tư, ','Thứ Năm, ','Thứ Sáu, ','Thứ Bảy, ','Chủ nhật, '),
			en : new Array('Monday, ','Tuesday, ','Wednesday, ','Thursday, ','Friday, ','Saturday, ','Sunday, ')
		};
	}
	var dateName = {
	    zh : new Array(' 日 '),
	    vi : new Array(' ngày '),
	    en : new Array(' day ')
	};
	var monthName = {
	    zh : new Array(' 月  '),
	    vi : new Array(' tháng '),
	    en : new Array(' month ')
	};
	var yearName = {
	    zh : new Array(' 年  '),
	    vi : new Array(' năm '),
	    en : new Array(' year ')
	};
	var hourName = {
	    zh : new Array(' 時間', ' 時間'),
	    vi : new Array(' Sáng', ' Chiều'),
	    en : new Array(' AM', ' PM')
	};
	var today    = new Date();
	var dayAt = (today.getDay()-startAt==-1)?6:(today.getDay()-startAt);
	var temp;
	dt.setSeconds(dt.getSeconds()+1);
	// format display
	// day
	if (days < 10) {
		days = "0" + days;
	}
	// hour
	if (hours < 12) {
		temp = hourName[lang][0];
	}
	else {
		temp = hourName[lang][1];
	}
	if (hours > 12) {
		hours = hours - 12;
	}
	if (hours == 0) {
		hours = 12;
	}
	if (hours < 10) {
	    	hours = "0" + hours;
	}
	// minute
	if (minutes < 10) {
		minutes = "0" + minutes;
	}
	// second
	if (seconds < 10) {
		seconds = "0" + seconds;
	}
	// show date time to page
	if (flag != null) {
		if (flag.value == "0")
			elem.innerHTML = dayName[lang][dayAt] + ", " + days + "-" + arrMonthName[months] + "-" + years + " " + hours + ":" + minutes + ":" + seconds + " " + temp;
		else if (flag.value == "1") {
			months = months + 1;
			if (months < 10)
				months = "0" + months;
			if (lang == "vi")
			    elem.innerHTML = dayName[lang][dayAt] + days + "/" + months + "/" + years// + " - " + hours + ":" + minutes + ":" + seconds + " " + temp;
			else if (lang == "zh")
			    elem.innerHTML = years + yearName[lang][0] + months + monthName[lang][0] + days + dateName[lang][0] + dayName[lang][dayAt]// + " _ " + hours + ":" + minutes + ":" + seconds + " " + temp;
			else if (lang == "en")
			    elem.innerHTML = dayName[lang][dayAt] + years + yearName[lang][0] + months + monthName[lang][0] + days + dateName[lang][0] + dayName[lang][dayAt]
			else
			    elem.innerHTML = dayName[lang][dayAt] + dateName[lang][0] + days + monthName[lang][0] + months + yearName[lang][0] + years// + " - " + hours + ":" + minutes + ":" + seconds + " " + temp;
		} else if (flag.value == "2") {
		    if (lang == "vi")
		        elem.innerHTML = days + "-" + arrMonthName[months] + "-" + years + " " + hours + ":" + minutes + ":" + seconds + " " + temp;	
		    else if (lang == "zh")
			    elem.innerHTML = days + "-" + arrMonthName[months] + "-" + years + " " + hours + ":" + minutes + ":" + seconds + " " + temp;	
			else if (lang == "en")
			    elem.innerHTML = dayName[lang][dayAt] + days + "-" + arrMonthName[months] + "-" + years + " " + hours + ":" + minutes + ":" + seconds + " " + temp;	
			else
			    elem.innerHTML = days + "-" + arrMonthName[months] + "-" + years + " " + hours + ":" + minutes + ":" + seconds + " " + temp;	
		}
		else if (flag.value == "3") {
			months = months + 1;
			if (months < 10)
				months = "0" + months;
			elem.innerHTML = days + "/" + months + "/" + years + " " + hours + ":" + minutes + ":" + seconds + " " + temp;
		} else if (flag.value == "4") {
			months = months + 1;
			if (months < 10)
				months = "0" + months;
			elem.innerHTML = months + "/" + days + "/" + years + " " + hours + ":" + minutes + ":" + seconds + " " + temp;
		} else if (flag.value == "5") {
			months = months + 1;
			if (months < 10)
				months = "0" + months;
			elem.innerHTML = years + "-" + months + "-" + days + " " + hours + ":" + minutes + ":" + seconds + " " + temp;
		} else if (flag.value == "6") {
			months = months + 1;
			if (months < 10)
				months = "0" + months;
			if (years > 2000)
				years = years - 2000;
			if (years < 10)
				years = "0" + years;
			elem.innerHTML = years + "/" + months + "/" + days + " " + hours + ":" + minutes + ":" + seconds + " " + temp;
		} else if (flag.value == "7") {
			if (years > 2000)
				years = years - 2000;
			if (years < 10)
				years = "0" + years;
			elem.innerHTML = days + "-" + arrMonthName[months] + "-" + years + " " + hours + ":" + minutes + ":" + seconds + " " + temp;
		} else if (flag.value == "8") {
			months = months + 1;
			if (months < 10)
				months = "0" + months;
			if (years > 2000)
				years = years - 2000;
			if (years < 10)
				years = "0" + years;
			elem.innerHTML = months + "/" + days + "/" + years + " " + hours + ":" + minutes + ":" + seconds + " " + temp;
		} else {
			elem.innerHTML = days + "-" + arrMonthName[months] + "-" + years + " " + hours + ":" + minutes + ":" + seconds + " " + temp;
		}
	} else {
		elem.innerHTML = days + "-" + arrMonthName[months] + "-" + years + " " + hours + ":" + minutes + ":" + seconds + " " + temp;
	}
	// replace time
	timerID = setTimeout("ShowClock()",1000);
	timerRunning = true;
}